package com.bn.userprofile;

import java.util.Map;

import atg.commerce.CommerceException;
import atg.commerce.order.OrderTools;
import atg.commerce.profile.CommercePropertyManager;
import atg.core.util.Address;
import atg.core.util.StringUtils;
import atg.projects.store.profile.StoreProfileTools;
import atg.repository.RepositoryException;
import atg.repository.RepositoryItem;

public class BNProfileTools extends  StoreProfileTools{

	/**
	 * Method used to delete an address from the address book based on a nick name.
	 *
	 * @param pProfile
	 * @param pAddressName
	 * @throws RepositoryException
	 */

	public void deleteAddressFromAddressBook(RepositoryItem pProfile, String pAddressName) throws RepositoryException {

		CommercePropertyManager commercePropertyManager = (CommercePropertyManager) getCommercePropertyManager();

		Map secondaryAddressMap = (Map) pProfile.getPropertyValue(commercePropertyManager.getSecondaryAddressPropertyName());
		if (secondaryAddressMap.containsKey(pAddressName)) {
			RepositoryItem address = getProfileAddress(pProfile, pAddressName);
			secondaryAddressMap.remove(pAddressName);
			updateProperty(commercePropertyManager.getSecondaryAddressPropertyName(), secondaryAddressMap, pProfile);

			// If deleted address is set as default, remove that as well
			String repositoryId = address.getRepositoryId();

			RepositoryItem defaultShipAddress = getDefaultShippingAddress(pProfile);

			if ((defaultShipAddress == null) || (!(repositoryId.equalsIgnoreCase(defaultShipAddress.getRepositoryId())))) {
				return;
			}
			updateProperty(commercePropertyManager.getShippingAddressPropertyName(), null, pProfile);
		}
	}

	public String isAddressExists(RepositoryItem pProfile, Address addressObject)
			throws RepositoryException {
		CommercePropertyManager cpmgr = (CommercePropertyManager) getPropertyManager();
		Map<String, RepositoryItem> mymap = (Map) pProfile
				.getPropertyValue(cpmgr.getSecondaryAddressPropertyName());

		String addressID =null;
		if (mymap == null || mymap.size() <= 0) {
			return addressID;
		}
		try {

			String newAddressString = convertAddressToString(addressObject);

			for (RepositoryItem addressItem : mymap.values()) {
				Address savedAddress = new Address();

				try {
					OrderTools.copyAddress(addressItem, savedAddress);
					String oldAddressString = convertAddressToString(savedAddress);

					if (newAddressString.equalsIgnoreCase(oldAddressString)) {
						addressID = addressItem.getRepositoryId();
						break;
					}

				} catch (CommerceException e) {
					vlogError("BNProfileTools::addressExists()-- Exception while comparing address"+e.getMessage());
				}
			}
		} catch (Exception e) {
			vlogError("BNProfileTools::addressExists()-- Exception while comparing address"+e.getMessage());
		}
		return addressID;
	}

	private String convertAddressToString(Address pAddress) {
		StringBuilder strBuilder = new StringBuilder();

		strBuilder.append(pAddress.getAddress1());
		if(!StringUtils.isBlank(pAddress.getAddress2())){
			strBuilder.append(pAddress.getAddress2());
		}
		strBuilder.append(pAddress.getFirstName());
		strBuilder.append(pAddress.getLastName());
		strBuilder.append(pAddress.getCity());
		strBuilder.append(pAddress.getState());
		strBuilder.append(pAddress.getCountry());
		strBuilder.append(pAddress.getPostalCode());

		return strBuilder.toString().toLowerCase().replaceAll(" ", "");
	}

}
